package com.xiaoxi.codeimageserver.controller;

import com.xiaoxi.codeimageserver.domain.ImageCode;
import com.xiaoxi.codeimageserver.domain.vo.ImageCodeVO;
import com.xiaoxi.codeimageserver.service.CodeImageService;
import com.xiaoxi.common.constant.constant.CommonConstants;
import com.xiaoxi.common.constant.constant.TimeConstants;
import com.xiaoxi.common.constant.enumeration.statuscode.user.ValidateCodeUserEnums;
import com.xiaoxi.common.response.ApiResponse;
import com.xiaoxi.common.util.RedisUtils;
import com.xiaoxi.common.util.UUIDUtils;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author xiaoxi
 * @ProjectName springcloudbms
 * @PackageName com.xiaoxi.codeimage.controller
 * @ClassName CodeImageController
 * @Description TODO
 * @Date 2023/2/2 19:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/code/api/v1")
@Slf4j
public class CodeImageController {

    @Resource
    private CodeImageService codeImageService;

    @Resource
    private RedisUtils redisUtils;

    @GetMapping("getVerificationCode")
    public ApiResponse createCode() {
        ImageCode imageCode = null;
        try {
            imageCode = codeImageService.createImageCode();
        } catch (IOException ioException) {
            log.error("图片验证码出现异常：{}", ioException.getMessage());
            return ApiResponse.ofSuccess();
        }
        String uuid = UUIDUtils.getRandomUUID();
        redisUtils.set(CommonConstants.IMAGE_CODE + uuid, imageCode, TimeConstants.VALIDATE_CODE);
        log.info("uuid : {} ---- 验证码 ： {}", uuid, imageCode.getCode());
        return ApiResponse.ofSuccess(new ImageCodeVO(imageCode.getImage(), uuid));
    }

    /**
     * @param verificationCodeId uuid
     * @param codeInRequest      用户提供验证码
     * @return
     */
    @GetMapping("checkVerificationCode")
    public ApiResponse checkVerificationCode(@Param("verificationCodeId") String verificationCodeId, @Param("codeInRequest") String codeInRequest) {

        // 从请求中取出之前存入redis的验证码
        ImageCode codeInRedis = (ImageCode) redisUtils.get(CommonConstants.IMAGE_CODE + verificationCodeId);

        // 用户提供验证码为空
        if (StringUtils.isEmpty(codeInRequest)) {
            return ApiResponse.ofEnum(ValidateCodeUserEnums.THE_VERIFICATION_CODE_CANNOT_BE_EMPTY);
        }

        // 系统验证码 无效
        if (Objects.isNull(codeInRedis)) {
            return ApiResponse.ofEnum(ValidateCodeUserEnums.THE_VERIFICATION_CODE_IS_INVALID);
        }

        // 系统验证码过期
        if (LocalDateTime.now().isAfter(codeInRedis.getExpireTime())) {
            return ApiResponse.ofEnum(ValidateCodeUserEnums.THE_VERIFICATION_CODE_HAS_EXPIRED);
        }

        // 验证码不匹配
        if (!StringUtils.equals(codeInRedis.getCode(), codeInRequest)) {
            redisUtils.remove(verificationCodeId);
            return ApiResponse.ofEnum(ValidateCodeUserEnums.VERIFICATION_CODES_DO_NOT_MATCH);
        }

        redisUtils.remove(verificationCodeId);
        return ApiResponse.ofSuccess();
    }

}
